function export_html(path,js) {
    var html_template = '\
<!DOCTYPE html>\
<html>\
    <head>\
        <title>{title}</title>\
        <meta charset="utf-8">\
        <script src="./ppt.js"></script>\
        <link rel="stylesheet" href="./ppt.css">\
        <link rel="stylesheet" href="./animate.css">\
        <link rel="stylesheet" href="./theme.css">\
    </head>\
    <body>\
        <div id="body">\
            <div id="ppt">\
                {html}\
            </div>\
        </div>\
        <script>\
            {js}\
            ppt.init();\
        </script>\
    </body>\
</html>\
';
    var body_html = "";
    for (var i = 0; i < pages.length; i++) {
        var page = pages[i];
        body_html += page.to_html();
    }
    var html = format(
        html_template,
        {
            title: "Idea3D-Document",
            html: body_html,
            js:js
        }
    );
    var fs = require('fs');
    fs.writeFileSync(path, html);
}

function export_all(path) {
    var fs = require('fs');
    if(fs.existsSync(path+'/doc')){
        rmdir(path+'/doc');
    }
    fs.mkdirSync(path + '/doc');
    var a_list=[];
    for(var i=0;i<window.animations_list.length;i++){
        a_list.push('"'+window.animations_list[i]+'"');
    }
    var js=
    'new_tags=['+String(a_list)+']\n'+
    'for(var i=0;i<new_tags.length;i++){'+
    '    ppt.animate_tags.push(new_tags[i])'+
    '}';
    
    export_html(path + "/doc/index.html",js);
    fs.writeFileSync(path + "/doc/theme.css", window.css);
    fs.writeFileSync(path + "/doc/animate.css", fs.readFileSync('./assets/doc/animate.css')+window.animate_css);
    fs.writeFileSync(path + "/doc/ppt.css", fs.readFileSync('./assets/doc/ppt.css'));
    fs.writeFileSync(path + "/doc/ppt.js", fs.readFileSync('./assets/doc/ppt.js'));
}

function exportf() {
    var remote = require('electron').remote;
    var dialog = remote.require('electron').dialog;
    var path = dialog.showOpenDialogSync({
        title: '导出',
        properties: ['openDirectory']
    })[0];
    export_all(path);
}

function deepCopy(obj) {
    var set = new Set();
    function deepClone(data) {
        var type = getType(data);
        var obj;
        if (type === 'array') {
            obj = [];
        } else if (type === 'object') {
            obj = {};
        } else {
            //不再具有下一层次
            return data;
        }
        if (type === 'array') {
            if (set.has(data)) return {};
            set.add(data);
            for (var i = 0, len = data.length; i < len; i++) {
                obj.push(deepClone(data[i]));
            }
        } else if (type === 'object') {
            if (set.has(data)) return {};
            set.add(data);
            for (var key in data) {
                obj[key] = deepClone(data[key]);
            }
        }
        return obj;
    }
    return deepClone(obj);
}


function save() {
    var j_obj = deepCopy(pages);
    for (var i = 0; i < j_obj.length; i++) {
        var objs = j_obj[i].objs;
        for (var j = 0; j < objs.length; j++) {
            var animates = objs[j].animations;
            for (var k = 0; k < animates.length; k++) {
                var animate = animates[k];
                animate.parent = {};
            }
        }
    }
    var json_obj = {
		title: window.filename,
        pages: j_obj,
        theme: window.css,
        animate:{
            css:window.animate_css,
            names:window.animations_list
        }
    };
    var data = JSON.stringify(json_obj);
    var remote = require('electron').remote;
    var dialog = remote.require('electron').dialog;
    if(!(window.file_path&&window.file_path!='')){
        window.file_path = dialog.showSaveDialogSync({
            title: '保存文件',
            filters: [{
                name: 'ide',
                extensions: ['ida']
            },
            {
                name: 'All Files',
                extensions: ['*']
            }
            ]
        });
		var patharr=window.file_path.split('\\');
		window.filename=patharr[patharr.length-1];
		$('title').innerText=window.filename;
    }
    var fs = require('fs');
    fs.writeFileSync(window.file_path, data);
    tip('保存成功!','success','成功:')
}

function rmdir(url) {
    var files = [];
    var fs=require('fs');
    var path=require('path');
    if (fs.existsSync(url)) {
        files = fs.readdirSync(url);
        files.forEach(function (file, index) {
            var curPath = path.join(url, file);
            if (fs.statSync(curPath).isDirectory()) { // recurse
                deleteFolderRecursive(curPath);
            } else {
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(url);
    }
}
function preview() {
    var path = process.env.TMP;
    export_all(path);
    var preview_window=window.open(path + "/doc/index.html");
    preview_window.eval('window.location.reload();');
    preview_window.maximize();
}
function reset(){
	$('.box').innerHTML="";
	pages=[];
}
function openf() {
    reset()
    var p = prompt;
    prompt = (a, b, f) => {};
	var a = alertWindow;
	alertWindow = (a,f)=>{};
    var remote = require('electron').remote;
    var dialog = remote.require('electron').dialog;
    var fs = require('fs');
    var path = dialog.showOpenDialogSync({
        title: "打开"
    })[0];
    window.file_path=path;
    var data = fs.readFileSync(path, { encoding: "utf-8" });
    var json = JSON.parse(data);
    var json_obj = json.pages;
    window.css = json.theme;
    window.animate_css=json.animate.css;
    window.animations_list=json.animate.names;
    for (var i = 0; i < json_obj.length; i++) {
        create_page();
        var page = pages[pages.length - 1];
        var page_v = json_obj[i];
        page.x = page_v.x;
        page.y = page_v.y;
        page.z = page_v.z;
        page.rotate_x = page_v.rotate_x;
        page.rotate_y = page_v.rotate_y;
        page.rotate_z = page_v.rotate_z;
        page.scale = page_v.scale;
        page.bg = page_v.bg;
        page.has_back = page_v.has_back;
        for (var j = 0; j < json_obj[i].objs.length; j++) {
            var obj_v = json_obj[i].objs[j];
            var obj;
            if (obj_v.type[0] == 'h') {
                obj = create_h(obj_v.type[1]);
                obj.color = obj_v.color;
                obj.dom.style.color = obj.color;
                obj.width = obj_v.width;
                obj.dom.style.width = obj.width;
            } else if (obj_v.type == "p") {
                obj = create_p();
                obj.color = obj_v.color;
                obj.dom.style.color = obj.color;
                obj.width = obj_v.width;
                obj.dom.style.width = obj.width+'px';
            } else if (obj_v.type == "img") {
                obj = create_img();
                obj.src = obj_v.src;
                obj.dom.setAttribute('src', obj.src);
                obj.width = obj_v.width;
                obj.height = obj_v.height;
                obj.dom.style.width = obj_v.width;
                obj.dom.style.height = obj_v.height;
                obj.show_src=obj_v.show_src;
            } else if (obj_v.type = "a") {
                obj = create_link();
                obj.href = obj_v.href;
                obj.dom.setAttribute('data-href', obj.href);
                obj.color = obj_v.color;
                obj.dom.style.color = obj.color;
                obj.width = obj_v.width;
                obj.dom.style.width = obj.width;
            }
            obj.value = obj_v.value;
            obj.x = obj_v.x;
            obj.y = obj_v.y;
            obj.update_pos();
            obj.update_value();
            current_element = obj;
            for (var k = 0; k < obj_v.animations.length; k++) {
                var animation_v = obj_v.animations[k];                
                var animation = add_animation(animation_v.name);
                animation.delay = animation_v.delay;
                animation.time = animation_v.time;
                animation.update_value();
            }
            current_element = null;
        }
    }
    prompt = p;
	alertWindow = a;
}
function new_file(){
	reset();
	window.filename='Untitled.ida';
	$('title').innerText=window.filename;
	create_page();
}